<?php
namespace app\validators;

use Yii;
use app\models\Member;
use app\models\SysConfig;
use yii\validators\Validator;

/**
 * 短信验证码验证器
 * Class SmscodeValidator
 */
class ChangepwrightValidator extends Validator
{
  /**
	 * @param \yii\base\Model $model
	 * @param string $attribute
	 */
	public function validateAttribute($model, $attribute)
	{
	   $user = $model->$attribute;
		 $member = Member::find()->where([
			'name' => $user,
		 ])->one();
		if(is_null($member)){
			$this->addError($model, $attribute,'用户不存在');
      return;
		}
    if(Yii::$app->user->getId()===$member->id){
      //允许修改自己的密码
      return;
    }
    $sysconfig = SysConfig::getSysConfig();
    if(SysConfig::curUserIsAdmin($sysconfig)){
      //允许管理员修改任何人的密码
      return;
    }
    $this->addError($model, $attribute,'无修改权限');
	}

}
